package com.qf.controller;

import com.qf.service.DtsGoodsProductService;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/dashboard")
public class AdminDashboardController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsGoodsProductService productService;

    @Autowired
    DtsOrderService orderService;

    private static final int STATIS_DAYS_RANG = 30;// 统计的天数范围，一个月数据


    @RequestMapping("/chart")
    public Object chart(){
        //商品分类和对应分类的销售额
        CategorySellVo categorySell = new CategorySellVo();
        //大类销售金额的集合(包含 大类的名称 ，销售金额)
        List<CategorySellAmts> catgoryShellData = orderService.findCategoryShellData();
        //提取 集合中的 大类的名称
        String[] categoryNames = fetchCategoryName(catgoryShellData);

        categorySell.setCategoryNames(categoryNames);
        categorySell.setCategorySellData(catgoryShellData);



        //查询一个月内，每天的用户新增量
        List<DayStatis> userStatis = userService.queryDayStatis(STATIS_DAYS_RANG);
        //查询一个月内 ， 包括每天的订单数量和总额
        List<DayStatis> orderStatis = orderService.queryDayStatis(STATIS_DAYS_RANG);

        //日期和对应的订单总额和订单笔数
        OrderAmtsVo orderAmts  = new OrderAmtsVo();
        //提取 订单 的日期 ， 订单金额， 订单数量
        String[] orderDayStrs = fetchDayStr(orderStatis);
        BigDecimal[] orderAmtData = fetchOrderAmt(orderStatis);
        int[] orderCntData = fetchOrderCnt(orderStatis);
        //组装数据
        orderAmts.setOrderAmtData(orderAmtData);
        orderAmts.setOrderCntData(orderCntData);
        orderAmts.setDayData(orderDayStrs);

        //日期和对应的用户数和订单数
        UserOrderCntVo userOrderCnt = new UserOrderCntVo();
        //一个月的日期
        //获取用户的日期
        String[] userDayStrs = fetchDayStr(userStatis);
        //把订单和用户 的日期数据合并
        String[] dayStrs = unionDayStr(userDayStrs, orderDayStrs);
        //订单增长量
        int[] orderCnt = fetchOrderCnt(dayStrs, orderStatis);
        //用户的增长量
        int[] userCnt = fetchUserCnt(dayStrs, userStatis);

        userOrderCnt.setOrderCnt(orderCnt);
        userOrderCnt.setUserCnt(userCnt);
        userOrderCnt.setDayData(dayStrs);

        Map<String,Object> data = new HashMap<>();
        data.put("categorySell", categorySell);
        data.put("orderAmts", orderAmts);
        data.put("userOrderCnt", userOrderCnt);

        return ResponseUtil.ok(data);
    }

    private int[] fetchUserCnt(String[] dayStrs, List<DayStatis> userStatis) {
        int[] userCnt = new int[dayStrs.length];
        for (int i = 0; i < dayStrs.length; i++) {
            for (int j = 0; j < userStatis.size(); j++) {
                DayStatis userStat =userStatis.get(j);
                if(dayStrs[i].equals(userStat.getDayStr())){
                    userCnt[i] = userStat.getCnts();
                    break;
                }
            }
        }
        return userCnt;
    }

    private int[] fetchOrderCnt(String[] dayStrs, List<DayStatis> orderStatis) {
        int[] orderCnt = new int[dayStrs.length];
        for (int i = 0; i < dayStrs.length; i++) {
            for (int j = 0; j < orderStatis.size(); j++) {
                DayStatis orderStat =orderStatis.get(j);
                if(dayStrs[i].equals(orderStat.getDayStr())){
                    orderCnt[i] = orderStat.getCnts();
                    break;
                }
            }
        }
        return orderCnt;
    }

    private String[] unionDayStr(String[] userDayStrs, String[] orderDayStrs) {
        //自动去重， 还会自动升序排序
        TreeSet<String> set = new TreeSet();

        for (String userDayStr : userDayStrs) {
            set.add(userDayStr);
        }

        for (String orderDayStr : orderDayStrs) {
            set.add(orderDayStr);
        }

        String[] dayStrs = set.toArray(new String[0]);

        return dayStrs;
    }


    private int[] fetchOrderCnt(List<DayStatis> statis) {
        int[] orderCnts = new int[statis.size()];

        for (int i = 0; i < statis.size(); i++) {
            orderCnts[i] = statis.get(i).getCnts();
        }
        return orderCnts;
    }

    private BigDecimal[] fetchOrderAmt(List<DayStatis> statis) {
        BigDecimal[] orderAmts = new BigDecimal[statis.size()];

        for (int i = 0; i < statis.size(); i++) {
            orderAmts[i] = statis.get(i).getAmts();
        }
        return orderAmts;
    }

    private String[] fetchDayStr(List<DayStatis> statis) {

        String[] dayStrs = new String[statis.size()];

        for (int i = 0; i < statis.size(); i++) {
            dayStrs[i] = statis.get(i).getDayStr();
        }
        return dayStrs;
    }

    private String[] fetchCategoryName(List<CategorySellAmts> catgoryShellData) {

        String[] names = new String[catgoryShellData.size()];

        for (int i = 0 ; i < catgoryShellData.size() ; i ++) {
            CategorySellAmts categorySellAmts = catgoryShellData.get(i);
            String name = categorySellAmts.getName();
            names[i] = name;
        }
        return names;
    }

    @GetMapping("")
    public Object info(){
        Integer goodsTotal = goodsService.findTotal();
        Integer userTotal = userService.findTotal();
        Integer productTotal = productService.findTotal();
        Integer orderTotal = orderService.findTotal();

        Map<String,Object > data = new HashMap<>();
        data.put("goodsTotal", goodsTotal);
        data.put("userTotal", userTotal);
        data.put("productTotal", productTotal);
        data.put("orderTotal", orderTotal);
        return ResponseUtil.ok(data);
    }

}
